<?php

/**
 * This is the model class for table "zy_potential_customers_list".
 *
 * The followings are the available columns in table 'zy_potential_customers_list':
 * @property integer $potential_customers_list_id
 * @property string $potential_customers_list_name
 *
 * The followings are the available model relations:
 * @property ZyPotentialCustomers[] $zyPotentialCustomers
 */
class PotentialCustomersList extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PotentialCustomersList the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_potential_customers_list';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('potential_customers_list_name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('potential_customers_list_id, potential_customers_list_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'zyPotentialCustomers' => array(self::HAS_MANY, 'ZyPotentialCustomers', 'potential_customers_list_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'potential_customers_list_id' => Yii::t('base','Potential Customers List'),
			'potential_customers_list_name' => Yii::t('base','Potential Customers List Name'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('potential_customers_list_id',$this->potential_customers_list_id);
		$criteria->compare('potential_customers_list_name',$this->potential_customers_list_name,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//通过客户名单列表id得到名字
	public function getListName($id)
	{
	//$potentialcustomerslist=PotentialCustomersList::model()->findByPk($id);
		$listname=array();
		$arr = str_replace("|", ",", $id);	
		$sql="select * from zy_potential_customers_list where potential_customers_list_id in ($arr)";
		$result=Yii::app()->db->createCommand($sql)->queryAll();
		for($i=0;$i<count($result);$i++)
		{
		$listname[$i]=$result[$i]['potential_customers_list_name'];
		//echo $result[$i]['potential_customers_list_name'].' ';
		}
		$customerslistname=implode('|',$listname);
	return $customerslistname;
	}
	
	//通过客户id找到客户列表的名字
	public function getPotentialCustomerListName($id)
	{
	$potentialcustomer=PotentialCustomers::model()->findByPk($id);
	$potentialcustomerlist=PotentialCustomersList::model()->findByPk($potentialcustomer->potential_customers_list_id);
	return $potentialcustomerlist->potential_customers_list_name;
	}
	
	
	/**
	 * 获取活动列表
	 */
	public function getActivityList(){
		
		$sql="select * from zy_activity";
		$result = yii::app()->db->createCommand($sql)->queryAll();
		return $result;
	}
	/**
	 * 获取拨打电话客户列表
	 * @return unknown
	 */
	public function getCustomersList(){
		$sql="select * from zy_potential_customers_list";
		$result = yii::app()->db->createCommand($sql)->queryAll();
		return $result;
	}
	
	/**
	 * 获取参与活动的调查人员
	 */
	public function getAcUser($code,$customers_list_id){
		$error_num=0;
		$success_num=0;
		$sql="SELECT * FROM zy_survey_user WHERE activety_code='".$code."' and mobile IS NOT NULL";
		$result = yii::app()->db->createCommand($sql)->queryAll();
		for ($i=0; $i<count($result); $i++){
			//查询号码是否在zy_potential_customers表中存在
			$sql_query_mobile="select * from zy_potential_customers where mobile='".$result[$i][mobile]."' and potential_customers_list_id='".$customers_list_id."'  limit 1";
			$rs = yii::app()->db->createCommand($sql_query_mobile)->queryAll();
			if(empty($rs)){
				$sql_insert="INSERT INTO `zy_potential_customers`(`potential_customers_list_id`,`first_name`,`last_name`,`gender`,`mobile`,`email`,`address`)
				VALUES ($customers_list_id,'".$result[$i][first_name]."','".$result[$i][last_name]."','','".$result[$i][mobile]."','".$result[$i][email]."','".$result[$i][full_street_address]."')";
				$result_num = yii::app()->db->createCommand($sql_insert)->execute();
				if($result_num){
					$success_num = $success_num+1;
				}else{
					$error_num = $error_num+ 1;
				}
			}
		}
		$message="\n Success Number:".$success_num."\n Error Number:".$error_num;
		return $message;
	}
	
	
	/**
	 * 获取电话表中人员.
	 */
	public function getCustomers($id){
		//加载PHPexcel类文件
		Yii::$enableIncludePath = false;
		Yii::import('application.extensions.PHPExcel', 1);
		//获取客户名单列表名称
		$potential_customers_list_name = PotentialCustomers::model()->getCustomersListName($id);
		
		$sql="select * from zy_potential_customers where potential_customers_list_id='".$id."'";
		$rs = yii::app()->db->createCommand($sql)->queryAll();
		
		$objectPHPExcel = new PHPExcel();
		$objectPHPExcel->setActiveSheetIndex(0);
		//设置表头格式
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFill()->getStartColor()->setARGB('FFCCCCCC');
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setName('Candara');
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setSize(12);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
		$objectPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
		//报表头的输出
		$objectPHPExcel->getActiveSheet()->setCellValue('A1','Customers List');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
		$objectPHPExcel->getActiveSheet()->setCellValue('B1','First Name');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
		$objectPHPExcel->getActiveSheet()->setCellValue('C1','Last Name');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
		$objectPHPExcel->getActiveSheet()->setCellValue('D1','Gender');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
		$objectPHPExcel->getActiveSheet()->setCellValue('E1','Mobile');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
		$objectPHPExcel->getActiveSheet()->setCellValue('F1','Email');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(35);
		$objectPHPExcel->getActiveSheet()->setCellValue('G1','Address');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(60);
		$objectPHPExcel->getActiveSheet()->setCellValue('H1','Post Code');
		$objectPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
		//内容输出
		for ($i=0; $i < count($rs); $i++){
			
			$objectPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$potential_customers_list_name);
			$objectPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$rs[$i][first_name]);
			$objectPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$rs[$i][last_name]);
			$objectPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$rs[$i][gender]);
			$objectPHPExcel->getActiveSheet()->setCellValue('E'.($i+2),$rs[$i][mobile].' ');
			$objectPHPExcel->getActiveSheet()->setCellValue('F'.($i+2),$rs[$i][email]);
			$objectPHPExcel->getActiveSheet()->setCellValue('G'.($i+2),$rs[$i][address]);
			$objectPHPExcel->getActiveSheet()->setCellValue('H'.($i+2),$rs[$i][post_code]);
			
		}
		
		ob_end_clean();
		ob_start();
		 
		header('Content-Type : application/vnd.ms-excel');
		header('Content-Disposition:attachment;filename="'.'Customers Lists-'.date("j,m,Y").'.xls"');
		$objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
		$objWriter->save('php://output');
	}
}